package cn.zhl.lambda;

import java.util.function.Function;

/**
 * @program: demos
 * @description:
 * @author: 刘振华
 * @create: 2020-04-20 11:58
 **/
public class FunctionTest {
	public static void main(String[] args) {
//        int rs = compute(3, new Function<Integer, Integer>() {
//            @Override
//            public Integer apply(Integer integer) {
//                return integer+5;
//            }
//        });
//        int rs = compute(3, a -> a * 2);
//        int rs = compute(3, a -> {return 0;});
//        System.out.println(rs);
//        String s = convertString(12, i -> "my name is " + i);
//        System.out.println(s);
		Function<Integer, Integer> f1 = a -> a + 3;
		Function<Integer, String> f2 = a -> "my age is " + a;
//        String s = composeDemo(12, f1, f2);
//        System.out.println(s);
		String s = addThenDemo(13, f1, f2);
		System.out.println(s);
	}

	static int compute(int a, Function<Integer, Integer> function) {
		return function.apply(a);
	}

	static String convertString(int a, Function<Integer, String> function) {
		return function.apply(a);
	}

	static String composeDemo(int a, Function<Integer, Integer> f1, Function<Integer, String> f2) {
		return f2.compose(f1).apply(a);
	}

	static String addThenDemo(int a, Function<Integer, Integer> f1, Function<Integer, String> f2) {
		return f1.andThen(f2).apply(a);
	}
}
